/**
 * 
 */
$(function() {
	$("#txm").focus();
	$("#sl").keyup(function(event) {
		if (event.keyCode == 13) {
			saoma();
		}
	});
	$("#txm").keyup(function(event) {
		if (event.keyCode == 13) {
			saoma();
		}
	});

	$("#qxB").click(function() {
		clearForm();
	});

	$("#dzpB").click(function() {
		payOrder(1);
	});

	$("#xjpB").click(function() {
		payOrder(0);
	});

})

function lockButton() {
	$(".mybtn").attr("disabled", true);
	$("#txm").attr("disabled", true);
	$("#sl").attr("disabled", true);
}

function releaseButton() {
	$(".mybtn").attr("disabled", false);
	$("#txm").attr("disabled", false);
	$("#sl").attr("disabled", false);
}

function calculate() {
	var $heji = 0.0;
	$(".myzj").each(function(i) {
		var $t = 0.0;
		$heji += $t.add($(this).text());
	});
	$("#heji").text($heji);
}

function resetForm() {
	$("#sl").val("1");
	$("#txm").val("").focus();
}

function clearForm() {
	$(".styleTr").remove();
	$("#heji").text("0");
	resetForm();
}

function payOrder(pay) {
	if ($(".styleTr").length < 1) {
		alert("当前无任何商品！");
	} else {
		var $txms = "";
		$(".hiddenTxm").each(function(i) {
			$txms += $(this).val() + ",";
		});
		$txms = $txms.substr(0, $txms.length - 1);
		var $sls = "";
		$(".hiddenSl").each(function(i) {
			$sls += $(this).val() + ",";
		});
		$sls = $sls.substr(0, $sls.length - 1);
		$.ajax({
			url : "shouyin.do?method=createOrder",
			type : "post",
			data : {
				txms : $txms,
				sls : $sls,
				type : pay
			},
			error : function(request) {
				alert("请重新扫码");
				location.reload();
			},
			success : function(data) {
				if (data.status == 200) {
					showMsg("交易成功！", "center");
					clearForm();
				}
			}
		});
	}
}

function saoma() {
	var $sl = $("#sl").val() > 0 ? parseInt($("#sl").val()) : 0;
	if ($sl == 0) {
		alert("数量有误！");
		$("#txm").val("");
		return;
	}
	var $txm = $("#txm").val();
	if ($txm == null || $txm == "") {
		alert("条形码有误！");
		return;
	}
	lockButton();
	$
			.ajax({
				url : "shouyin.do?method=saoma",
				type : "POST",
				data : {
					txm : $txm,
					sl : $sl
				},
				error : function(request) {
					alert("出现问题，最好重启电脑！");
				},
				success : function(data) {
					if (data.status == 200) {
						var $name = data.name;
						var $dj = data.dj;
						var $zj = data.zj;
						$("#tBody")
								.append(
										'<tr class="styleTr"><td>'
												+ $name
												+ '</td><td>'
												+ $sl
												+ '</td><td>'
												+ $dj
												+ '</td><td class="myzj">'
												+ $zj
												+ '</td><td class="deleteTd"><pre onclick="deleteTr(this)" class="deleteSpan mySpan" title="删除">    </pre></td><input type="hidden" class="hiddenTxm" value="'
												+ $txm
												+ '" /><input type="hidden" class="hiddenSl" value="'
												+ $sl + '" /></tr>');
						calculate();
					} else {
						alert(data.msg);
					}
					releaseButton();
					resetForm();
				}
			});
}

function deleteTr(t) {
	$(t).parents(".styleTr").remove();
	calculate();
	$("#txm").val("").focus();
}

function accAdd(arg1, arg2) {
	var r1, r2, m;
	try {
		r1 = arg1.toString().split('.')[1].length
	} catch (e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split('.')[1].length
	} catch (e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2));
	return (arg1 * m + arg2 * m) / m;
}
Number.prototype.add = function(arg) {
	return accAdd(arg, this);
}